<?php
declare(strict_types=1);

namespace App\Service;

use App\Constants\XZXResponseCode;
use App\Exception\XZXValidationException;
use App\Model\Dao\MchAccountDao;
use App\Model\Dao\MchConfigDao;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use App\Utils\NumberUtils;
use Hyperf\Di\Annotation\Inject;


class MchAccountService
{

    /**
     * @Inject()
     * @var MchAccountDao
     */
    private $MchAccountDao;

    /**
     * @Inject()
     * @var MchConfigDao
     */
    private $MchConfigDao;

    public function get($param)
    {
        $result = $this->MchAccountDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        if($param['source'] == 2 && !$param['isSuperRole']){
            if(!in_array($result['mchId'],$param['mchIdList'])){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }

        (isset($result['createTime']) && !empty($result['createTime'])) && $result['createTime'] = DateUtils::getDateTime($result['createTime']);

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

    public function getForMch($param)
    {
        $result = $this->MchAccountDao->getByMchId($param['mchId']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        if($result['mchId'] != $param['mchId']){
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
        }

        $mchConfig = $this->MchConfigDao->getByMchId($param['mchId']);
        $data = [
            "mchNo"                    => $result['mchNo'],
            "settleCurrency"           => $mchConfig['settleCurrency'],
            'totalAccountCashAmount'   => $result['tradeCashAmount'],
            'totalAccountFrozenAmount' => ($result['tradeFrozenAmount'] + $result['bondFrozenAmount'] + $result['eventReviewFrozenAmount'] + $result['eventFrozenAmount']),
            "tradeCashAmount"          => $result['tradeCashAmount'],
            "tradeFrozenAmount"        => $result['tradeFrozenAmount'],
            "bondFrozenAmount"         => $result['bondFrozenAmount'],
            "eventReviewFrozenAmount"  => $result['eventReviewFrozenAmount'],
            "eventFrozenAmount"        => $result['eventFrozenAmount']
        ];

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), $data);
    }

    public function list(array $param)
    {

        $limit = $param['limit'];
        $page = $param['page'];        

        $total = $this->MchAccountDao->getCount($param);
        $list = $this->MchAccountDao->getList($param, $page, $limit);

        foreach ($list as $k => $v) {
            (isset($v['createTime']) && !empty($v['createTime'])) && $v['createTime'] = DateUtils::getDateTime($v['createTime']);

            $v['totalAccountCashAmount'] = $v['tradeCashAmount'];
            $v['totalAccountFrozenAmount'] = $v['tradeFrozenAmount'] + $v['bondFrozenAmount'] + $v['eventReviewFrozenAmount'] + $v['eventFrozenAmount'];

            $v['floatTotalAccountCashAmount'] = NumberUtils::centToYuan($v['totalAccountCashAmount']);
            $v['floatTotalAccountFrozenAmount'] = NumberUtils::centToYuan($v['totalAccountFrozenAmount']);
            $v['floatTradeFrozenAmount'] = NumberUtils::centToYuan($v['tradeFrozenAmount']);
            $v['floatBondFrozenAmount'] = NumberUtils::centToYuan($v['bondFrozenAmount']);
            $v['floatEventReviewFrozenAmount'] = NumberUtils::centToYuan($v['eventReviewFrozenAmount']);
            $v['floatEventFrozenAmount'] = NumberUtils::centToYuan($v['eventFrozenAmount']);
            $list[$k] = $v;
        }

        $data = array(
            'total' => $total,
            'list' => $list
        );

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$data);
    }    
}